package com.hndct.Exception;

import com.hndct.common.result.Result;
import com.hndct.common.result.ResultEnum;
import com.hndct.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ProjectName: ump-mine
 * @PachageName: com.hndct.Exception
 * @Author: YuZhenDong
 * @Discription: 统一异常处理
 * @Date: 2019/4/13
 * ClassName: ExcepitonHandle
*/

@RestControllerAdvice
public class ExcepitonHandle {
    private static final Logger logger = LoggerFactory.getLogger(ExcepitonHandle.class);

    /**
     * 功能描述:
     * @param e:
     * @Return: com.hndct.common.result.Result
     * @Auther: YuZhenDong
     * @Date: 2019/4/13
     * @Discription: 系统异常
     */
    @ExceptionHandler(value = GobalException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result handel(Exception e){
        if(e instanceof GobalException){
            GobalException gobalException =(GobalException)e;
            return ResultUtil.error( gobalException.getCode(),gobalException.getMessage());
        }else {
            logger.error("[系统异常] {}",e);
            return ResultUtil.error(ResultEnum.ERROR_SYSTEM);
        }
    }
}
